<template>
  <div class="global">
    <div>
      <el-input
        placeholder="宿舍编号"
        style="width: 200px !important"
        class="mr"
        v-model="rule.number"
      ></el-input>

      <el-button type="primary" class="mr" @click="cx">查询</el-button>
      <el-button type="primary" class="mr" @click="xz">新增</el-button>
      <el-upload
        class="avatar-uploader mr"
        action="http://192.168.0.105:9091/student/indown"
        :show-file-list="false"
        accept=".xlsx"
        style="display: inline-block; margin-left: 10px"
        :on-success="handleAvatarSuccess"
      >
        <el-button type="primary">导入</el-button>
      </el-upload>
      <el-button type="primary" @click="dc">导出</el-button>
    </div>
    <el-table :data="tabledata" border class="mt">
      <el-table-column label="宿舍编号" prop="number"></el-table-column>
      <el-table-column label="所属楼宇" prop="louyuname"></el-table-column>
      <el-table-column label="所属楼层" prop="sslc"></el-table-column>
      <el-table-column
        label="最大可住人数"
        prop="maxlive"
      ></el-table-column>
      <el-table-column label="已住人数" prop="haslive"></el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="bj(scope.row)">编辑</el-button>
          <el-button type="danger" @click="sc(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="title"
      width="30%"
      :visible="dialogVisible"
      :before-close="handleClose"
    >
      <el-form :model="serch" label-width="130">
        <el-form-item label="宿舍编号" prop="number">
          <el-input v-model="serch.number"></el-input>
        </el-form-item>

        <el-form-item label="所属楼宇" prop="louyuid">
          <el-select v-model="serch.louyuid" @change="xgss">
            <el-option
              :key="index"
              v-for="(item, index) in domilist"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属楼层" prop="sslc">
          <el-input v-model="serch.sslc"></el-input>
        </el-form-item>
        <el-form-item label="最大可住人数" prop="maxlive">
          <el-input v-model="serch.maxlive"></el-input>
        </el-form-item>
        <el-form-item label="已住人数" prop="haslive">
          <el-input v-model="serch.haslive"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="cz()">重置</el-button>
          <el-button type="primary" @click="save()">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="rule.pageNum"
      :page-size="rule.pageSize"
      layout="prev, pager, next"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import service from "@/request";
export default {
  data() {
    return {
      tabledata: [],
      dialogVisible: false,
      title: "新增",
      rule: {
        pageNum: 1,
        pageSize: 10,

        number: "",
      },
      serch: {
        id: "",
        number: "",
        sslc: "",
        haslive: "",
        maxlive: "",
        louyuid: "",
        louyuname: "",
      },
      total: 0,
      domilist: [],
    };
  },
  mounted() {
    this.getdata();
    this.getdata1();
    this.gettime();
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
    },
    xgss(e) {
      console.log(e);
      this.domilist.forEach((el) => {
        if (el.id == e) {
          this.serch.louyuname = el.name;
        }
      });
    },
    sc(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await service.delete(`/sushe/${row.id}`);
          console.log(res);
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
          this.getdata();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    cz() {
      this.serch.number = "";
      this.serch.sslc = "";
      this.serch.haslive = "";
      this.serch.maxlive = "";
      this.serch.louyuid = "";
      this.serch.louyuname = "";
    },
    bj(row) {
      this.serch = {
        id: row.id,
        number: row.number,
        sslc: row.sslc,
        louyuid: row.louyuid,
        louyuname: row.louyuname,
        haslive: row.haslive,
        maxlive: row.maxlive,
      };
      this.title = "修改";
      this.dialogVisible = true;
    },
    async save() {
      if (this.title == "新增") {
        const res = await service.post("/sushe/add", this.serch);
        console.log(res);
        if (res.code == 200) {
          this.$message.success(`${res.msg}`);
          this.dialogVisible = false;
          this.getdata();
        }
      } else {
        const res = await service.post("/sushe/update", this.serch);
        console.log(res);
        if (res.code == 200) {
          this.$message.success(`${res.msg}`);
          this.dialogVisible = false;
          this.getdata();
        }
      }
    },
    xz() {
      this.dialogVisible = true;
      this.title = "新增";
      this.serch = {
        number: "",
        sslc: "",
        louyuid: "",
        louyuname: "",
        haslive: "",
        maxlive: "",
      };
    },
    gettime() {
      // 格式化日对象
      var date = new Date();
      var sign2 = ":";
      var year = date.getFullYear(); // 年
      var month = date.getMonth() + 1; // 月
      var day = date.getDate(); // 日
      var hour = date.getHours(); // 时
      var minutes = date.getMinutes(); // 分
      var seconds = date.getSeconds(); //秒
      var weekArr = [
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
        "星期天",
      ];
      var week = weekArr[date.getDay()];
      // 给一位数的数据前面加 “0”
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (day >= 0 && day <= 9) {
        day = "0" + day;
      }
      if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
      }
      if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
      }
      if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
      }
      return (
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hour +
        sign2 +
        minutes +
        sign2 +
        seconds
      );
    },
    handleCurrentChange(e) {
      this.rule.pageNum = e;
      this.getdata();
    },
    handleSizeChange(e) {
      this.rule.pageSize = e;
      this.getdata();
    },
    cx() {
      this.rule.pageNum = 1;
      this.rule.pageSize = 10;
      this.getdata();
    },
    dc() {
      window.open("http://localhost:9091/sushe/down");
    },
    async getdata() {
      const res = await service.get("/sushe/index", {
        params: {
          pageNum: this.rule.pageNum,
          pageSize: this.rule.pageSize,
          number: this.rule.number,
        },
      });
      console.log(res);
      this.tabledata = res.data;
    },
    async getdata1() {
      const res = await service.get("/louyu/xl");
      console.log(res);
      this.domilist = res.data;
    },
    handleAvatarSuccess(e) {
      console.log(e);
    },
  },
};
</script>